package com.coursehub.course.repository;

import com.coursehub.course.entity.CourseTeacher;
import com.coursehub.course.entity.CourseTeacherId;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 课程教师关联数据访问层
 */
@Repository
public interface CourseTeacherRepository extends JpaRepository<CourseTeacher, CourseTeacherId>, JpaSpecificationExecutor<CourseTeacher> {

    /**
     * 根据课程ID查找所有课程教师关联
     */
    List<CourseTeacher> findByCourse_CourseId(String courseId);
    
    /**
     * 根据用户ID查找所有课程教师关联
     */
    List<CourseTeacher> findById_UserId(String userId);
    
    /**
     * 根据课程ID和用户ID查找课程教师关联
     */
    Optional<CourseTeacher> findById_CourseIdAndId_UserId(String courseId, String userId);
    
    /**
     * 根据课程ID和用户ID删除课程教师关联
     */
    void deleteById_CourseIdAndId_UserId(String courseId, String userId);

    /**
     * 检查课程教师关联是否存在
     */
    boolean existsById_courseIdAndId_userId(String courseId, String userId);

    /**
     * 检查课程教师关联是否存在（自定义查询）
     */
    @Query("SELECT EXISTS(SELECT ct FROM CourseTeacher ct WHERE ct.id.courseId = :courseId AND ct.id.userId = :userId)")
    boolean existsByCourseIdAndUserId(@Param("courseId") String courseId, @Param("userId") String userId);
}